package com.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 处理根据考试号删除单个考试的Servlet
 * @author 12270
 */
@WebServlet("/dots")
public class DeleteOneTestServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1. 获取请求参数
        //获取要删除的教师号
        String testid=req.getParameter("testid");
        
        //2. 执行业务逻辑
        //根据教师号从数据库删除考试
        String driver_class = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://localhost:1433;databaseName=OnlineTestSystem";
        String username = "sa";
        String password = "sa";

        Connection con = null;
        PreparedStatement ps = null;
        int rows=0;

        try {
            Class.forName(driver_class);
            con = DriverManager.getConnection(url, username, password);
            ps = con.prepareStatement("delete from test where testid=? and isTestfinish=0");
            ps.setString(1,testid);
            rows=ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                
                if (null!=ps) ps.close();
                if (null!=con) con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        
    }
}